﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using UMC.Data;

namespace UMC.Web
{


    public class UIClick : UMC.Data.IJSON
    {
        public String Text
        {
            get; set;
        }
        public static UIClick Search(String text, UIClick click)
        {
            return new UIClick() { Text = text, Key = "Search", _send = click };
        }
        public static UIClick Latlng(UIClick click)
        {
            return new UIClick() { Key = "Latlng", _send = click };
        }
        public static UIClick Latlng(String text, UIClick click)
        {
            return new UIClick() { Text = text, Key = "Latlng", _send = click };
        }
        public static UIClick Search(String text)
        {
            return new UIClick() { Text = text, Key = "Search" };
        }
        public static UIClick Pager(String model, String cmd, WebMeta search)
        {
            var key = new WebMeta().Put("model", model, "cmd", cmd).Put("search", search);
            return new UIClick(key) { Key = "Pager" };
        }
        public static UIClick Pager(String model, String cmd, WebMeta search, bool isCache)
        {

            var key = new WebMeta().Put("model", model, "cmd", cmd).Put("search", search);
            if (isCache)
            {
                key.Put("Cache", isCache);
            }
            return new UIClick(key) { Key = "Pager" };
        }
        public static UIClick Pager(String model, String cmd)
        {
            var key = new WebMeta().Put("model", model, "cmd", cmd);
            return new UIClick(key) { Key = "Pager" };
        }
        public static UIClick Pager(String model, String cmd, bool isCache, String closeEvent)
        {
            var key = new WebMeta().Put("model", model, "cmd", cmd);
            key.Put("CloseEvent", closeEvent);
            if (isCache)
            {
                key.Put("Cache", isCache);
            }
            return new UIClick(key) { Key = "Pager" };
        }
        public static UIClick Pager(String model, String cmd, WebMeta search, bool isCache, String closeEvent)
        {

            var key = new WebMeta().Put("model", model, "cmd", cmd).Put("search", search);
            key.Put("CloseEvent", closeEvent);
            if (isCache)
            {
                key.Put("Cache", isCache);
            }
            return new UIClick(key) { Key = "Pager" };
        }
        public static UIClick Pager(String model, String cmd, bool isCache)
        {
            var key = new WebMeta().Put("model", model, "cmd", cmd);
            if (isCache)
            {
                key.Put("Cache", isCache);
            }
            return new UIClick(key) { Key = "Pager" };
        }
        public static UIClick Pager(String model, String cmd, String refreshEvent)
        {
            var key = new WebMeta().Put("model", model, "cmd", cmd);
            key.Put("RefreshEvent", refreshEvent);
            return new UIClick(key) { Key = "Pager" };
        }
        public static UIClick Pager(String model, String cmd, WebMeta search, String refreshEvent)
        {
            var key = new WebMeta().Put("model", model, "cmd", cmd).Put("search", search);
            key.Put("RefreshEvent", refreshEvent);
            return new UIClick(key) { Key = "Pager" };
        }
        public static UIClick Url(Uri url)
        {
            return new UIClick(url.AbsoluteUri) { Key = "Url" };
        }
        public static UIClick Url(string url)
        {
            return new UIClick(url) { Key = "Url" };
        }
        public static UIClick Tel(String tel)
        {
            return new UIClick(tel) { Key = "Tel" };
        }
        public static UIClick Scanning()
        {
            return new UIClick() { Key = "Scanning" };
        }
        public static UIClick Map(String location, String address, params WebMeta[] items)
        {
            return new UIClick(new WebMeta().Put("location", location, "address", address).Put("items", items)) { Key = "Map" };
        }
        public static UIClick Map(String address, params WebMeta[] items)
        {
            if (items.Length > 0)
            {

                return new UIClick(new WebMeta().Put("address", address).Put("items", items)) { Key = "Map" };
            }
            else
            {
                return new UIClick(address) { Key = "Map" };
            }
        }
        public static UIClick Click(UIClick click)
        {
            var c = new UIClick() { Key = "Click" };
            c._send = click;
            return c;
        }
        public static UIClick Markdown(UIClick click)
        {
            var c = new UIClick() { Key = "Markdown" };
            c._send = click;
            return c;
        }


        /// <summary>
        /// 界面查询
        /// </summary>
        /// <param name="query">查询字典对</param>
        /// <returns></returns>
        public static UIClick Query(WebMeta query)
        {
            return new UIClick(query) { Key = "Query" };
        }
        public static UIClick UIEvent(string name, object value)
        {
            return new UIClick("Event", new WebMeta().Put("type", "UI.Event", "key", name).Put("value", value));

        }
        public static UIClick UIEvent(string name, string ui, object value)
        {
            switch ((ui ?? "none").ToLower())
            {
                case "":
                case "none":
                    return new UIClick("Event", new WebMeta().Put("type", "UI.Event", "key", name).Put("value", value));

                default:
                    return new UIClick("Event", new WebMeta().Put("type", "UI.Event", "key", name).Put("ui", ui).Put("value", value));

            }
        }
        /// <summary>
        /// 界面查询
        /// </summary>
        /// <param name="query">查询字典对</param>
        /// <param name="cache">是否缓存</param>
        /// <returns></returns>
        public static UIClick Query(WebMeta query, bool cache)
        {
            return cache ? new UIClick(new WebMeta(query).Put("cache", true)) { Key = "Query" } : new UIClick(query) { Key = "Query" };
        }
        /// <summary>
        /// 界面查询
        /// </summary>
        /// <param name="key">Section界面Key</param>
        /// <param name="query"></param>
        /// <returns></returns>
        public static UIClick Query(String key, WebMeta query)
        {
            return new UIClick(new WebMeta().Put("key", key).Put("search", query)) { Key = "Query" };
        }
        public UIClick() { }
        public UIClick(String send)
        {
            this._send = send;
        }
        public UIClick(String model, String cmd)
        {
            this.Command = cmd;
            this.Model = model;
        }
        public UIClick(String model, String cmd, string send)
        {
            this._send = send;
            this.Command = cmd;
            this.Model = model;
        }

        public UIClick(String key, WebMeta send)
        {
            this._send = send;
            this.Key = key;
        }
        public UIClick(String key, UIClick send)
        {
            this._send = send;
            this.Key = key;
        }
        public UIClick(WebRequest request, WebMeta send)
        {
            this._send = send;
            this.Command = request.Command;
            this.Model = request.Model;
        }
        public UIClick(String model, String cmd, WebMeta send)
        {
            this._send = send;
            this.Command = cmd;
            this.Model = model;
        }

        public UIClick(WebRequest request, params string[] keys)
        {
            this._send = new WebMeta(request.Arguments).Put(keys);

            this.Command = request.Command;
            this.Model = request.Model;
        }
        public UIClick(WebRequest request, string send)
        {

            this._send = send;
            this.Command = request.Command;
            this.Model = request.Model;
        }
        public UIClick(string model, string cmd, params String[] keys)
        {
            this._send = new WebMeta(keys);
            this.Command = cmd;
            this.Model = model;
        }
        public UIClick(WebMeta send)
        {
            this._send = send;
        }
        public UIClick Send(WebMeta send)
        {
            this._send = send;
            return this;

        }

        public UIClick Send(String send)
        {
            this._send = send;
            return this;

        }

        public UIClick Send(params String[] keys)
        {
            this._send = new WebMeta().Put(keys); ;
            return this;
        }
        public String Key
        {
            get; set;
        }
        private object _send;
        public String Model
        {
            get; set;
        }
        public String Command
        {
            get; set;
        }
        public String Value
        {
            get; set;
        }

        public Object Send()
        {
            return _send;

        }

        void IJSON.Read(string key, object value)
        {
            switch (key)
            {
                case "key":
                    this.Key = value as string;
                    break;
                case "send":
                    this._send = value;
                    break;
                case "model":
                    this.Model = value as string;
                    break;
                case "text":
                    this.Text = value as string;
                    break;
                case "cmd":
                    this.Command = value as string;
                    break;
            }
        }
        void IJSON.Write(System.IO.TextWriter writer)
        {
            writer.Write("{");
            if (String.IsNullOrEmpty(this.Key) == false)
            {
                UMC.Data.JSON.Serialize("key", writer); 
                writer.Write(":");
                UMC.Data.JSON.Serialize(this.Key, writer);
                if (this._send != null)
                {

                    writer.Write(",");
                    UMC.Data.JSON.Serialize("send", writer); writer.Write(":");
                    UMC.Data.JSON.Serialize(this._send, writer);


                }
            }
            else
            {

                UMC.Data.JSON.Serialize("model", writer);
                writer.Write(":");
                UMC.Data.JSON.Serialize(this.Model, writer);
                writer.Write(",");
                UMC.Data.JSON.Serialize("cmd", writer);
                writer.Write(":");
                UMC.Data.JSON.Serialize(this.Command, writer);

                if (this._send != null)
                {

                    writer.Write(",");
                    UMC.Data.JSON.Serialize("send", writer); writer.Write(":");
                    UMC.Data.JSON.Serialize(this._send, writer);


                }
            }
            if (String.IsNullOrEmpty(Value) == false)
            {
                writer.Write(",");

                UMC.Data.JSON.Serialize("value", writer); writer.Write(":");
                UMC.Data.JSON.Serialize(this.Value, writer);
            }

            if (String.IsNullOrEmpty(Text) == false)
            {
                writer.Write(",");
                UMC.Data.JSON.Serialize("text", writer); writer.Write(":");
                UMC.Data.JSON.Serialize(this.Text, writer);


            }
            writer.Write("}");
        }
    }
}